package com.ytkj.tradeserver.controller.admin.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.util.StringUtil;
import com.ytkj.tradeserver.constant.ComConstants;
import com.ytkj.tradeserver.entity.AppInfo;
import com.ytkj.tradeserver.entity.AttachmentFileVO;
import com.ytkj.tradeserver.model.PageBean;
import com.ytkj.tradeserver.service.iservice.IAppInfoService;
import com.ytkj.tradeserver.service.iservice.IAttachmentFileService;
import com.ytkj.tradeserver.utils.StringOpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.ytkj.tradeserver.controller.admin.IAdminController;
import com.ytkj.tradeserver.entity.ProductInfo;
import com.ytkj.tradeserver.entity.UserInfo;
import com.ytkj.tradeserver.service.IProductService;
import com.ytkj.tradeserver.service.iservice.IUserInfoService;
import com.ytkj.tradeserver.utils.MD5Helper;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController  {

	@Resource
	private IUserInfoService userInfoService;
	
	@Resource 
	private IProductService  iproductService;

	@Resource
	private IAttachmentFileService fileService;

	@Resource
	private IAppInfoService appInfoService;

	//注销
	@RequestMapping(value="/logout")
	public String logout(HttpServletRequest request){
		request.getSession(true).removeAttribute("userInfo");
		return "redirect:"+ComConstants.LOGIN_URL;
	}

	@ResponseBody
	@RequestMapping(value="/login",method = RequestMethod.POST)
	public Map<String,String> login(@RequestParam String username,@RequestParam String userpass,HttpSession httpSession) {
		Map<String,String> returnMsg = new HashMap<String,String>();

		if(StringUtil.isEmpty(username)||StringUtil.isEmpty(userpass))
		{
			returnMsg.put("code","0001");
			return returnMsg;
		}

		String changePwd = MD5Helper.createMD5(userpass + ComConstants.PWD_SUFFIX);
		UserInfo userInfo = new UserInfo(username,changePwd);
		         userInfo = userInfoService.getUserInfo(userInfo);

		if(userInfo==null){
			returnMsg.put("code","0002");
			return returnMsg;
		}

		httpSession.setAttribute("userInfo",userInfo);
		userInfoService.updateUserLoginTime(userInfo);
		returnMsg.put("code","0000");
		return returnMsg;
	}

	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return "admin/index";
	}

	@RequestMapping("/pushorder")
	public String pushorder() {
		return "admin/orderlist";
	}

	@RequestMapping(value="/applist",method = RequestMethod.GET)
	public ModelAndView applist(@RequestParam(value="pageNum",required = false,defaultValue=ComConstants.PAGENUM) int pageNum,
                                @RequestParam(value="pageSize",required = false,defaultValue=ComConstants.PAGESIZE) int pageSize,
                                AppInfo record, HttpServletRequest request) {
        String url = request.getContextPath() + request.getServletPath();
        PageBean<AppInfo> pageApps = appInfoService.queryAllAppInfo(url,record,new PageBean(pageNum,pageSize));
        return new ModelAndView("admin/applist","pageBean",pageApps);
	}

	@RequestMapping("/userlist")
	public String userlist() {
		return "admin/userlist";
	}

	@RequestMapping("/adlist")
	public String adlist() {
		return "admin/adlist";
	}

	@RequestMapping("/loglist")
	public String loglist() {
		return "admin/loglist";
	}

	@RequestMapping("/adduser")
	public String adduser() {

		return "admin/adduser";
	}

	@RequestMapping("/productlist")
	public String productlist() {
		// TODO Auto-generated method stub
		return "admin/productlist";
	}

	@RequestMapping("/register")
	public String register() {

		return "";
	}

	@RequestMapping("/addapp")
	public String addApp() {
		return "admin/addapp";
	}

	@RequestMapping("/uploadapp")
	public String appUdload(AppInfo appInfo, HttpServletRequest request) throws IOException {
		UserInfo userInfo = (UserInfo)request.getSession(true).getAttribute("userInfo");
		appInfo.setApauthor(userInfo.getId());
		appInfoService.insert(appInfo);
		fileService.saveSingleFileData(String.valueOf(appInfo.getId()),ComConstants.FILETYPE.APP.getValue(),userInfo,request);
		return "redirect:/admin/applist";
	}

	@RequestMapping("/uploadproduct")
	public String uploadproduct (@RequestParam(value="pname",defaultValue="") String pname,
			@RequestParam(value="pappid",defaultValue="")	String pappid,
			@RequestParam(value="psource",defaultValue="") String psource,
			@RequestParam(value="pcontact",defaultValue="") String pcontact,
			@RequestParam(value="tel",defaultValue="") String tel,
			@RequestParam(value="purl",defaultValue="") String purl,
			@RequestParam(value="pdesc",defaultValue="") String pdesc){
		
		ProductInfo  productInfo = new ProductInfo();
		productInfo.setPid(MD5Helper.createMD5(pappid));
		productInfo.setPname(pname);
		productInfo.setPsource(psource);
		productInfo.setPdesc(pdesc);
		productInfo.setPurl(purl);
		productInfo.setPtel(tel);
		productInfo.setPtime(System.currentTimeMillis()+"");
		productInfo.setPappid(pappid);
		productInfo.setPcontant(pcontact);
		productInfo.setPcount(3555);
	    
		System.out.println("========JSON.toJSONString========="+JSON.toJSONString(productInfo));
		
		String result = iproductService.insertProductInfo(productInfo);
		System.out.println("========result========="+result);
		if (result!=null ) {
			return productlist();
		}else {
			
			return "";
			
		}	
	}



}
